<?php
namespace App\Admin\Controllers\Basics;

use App\Admin\Actions\Basics\DisableItem;
use App\Admin\Actions\Basics\EnableItem;
use App\Models\Basics\SettlementMethod;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class SettlementMethodController extends AdminController{

    protected $title = '结算方式';

    protected function grid()
    {
        $grid = new Grid(new SettlementMethod);
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID')->sortable();
        $grid->column('settlement_method','结算方式');
        $grid->column('status','启用')->bool([1 => true, 2 => false])->filter(SettlementMethod::$_status);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });

        $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
            $batch->disableDelete();
            $batch->add(new DisableItem());
            $batch->add(new EnableItem());
        });
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->quickSearch('settlement_method');
        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->text('settlement_method', '结算方式');
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new SettlementMethod());

        $form->hidden('id');

        $form->text('settlement_method', '结算方式')
            ->setWidth(3)
            ->creationRules(['required', "unique:b_settlement_method"],['required' => '结算方式必填','unique' => '结算方式已存在'])
            ->updateRules(['required', "unique:b_settlement_method,settlement_method,{{id}}"],['required' => '结算方式必填','unique' => '结算方式已存在']);
        $form->switch('status', '启用')->states(statusSwitch())->default('1');
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
